package com.itheima.service.mongo.impl;

import cn.hutool.core.util.ObjectUtil;
import com.itheima.domain.mongo.RecommendUser;
import com.itheima.domain.mongo.UserLike;
import com.itheima.service.mongo.RecommendUserService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.security.cert.Certificate;
import java.util.List;

@DubboService
public class RecommendUserServiceImpl implements RecommendUserService {

    @Autowired
    private MongoTemplate mongoTemplate;


    //查询今日佳人
    @Override
    public RecommendUser findTodayBest(Long toUserId) {
        Query query = new Query(
                Criteria.where("toUserId").is(toUserId)
        ).with(Sort.by(Sort.Order.desc("score"))).skip(0).limit(1);

        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        return recommendUser;

    }
    // 分页查询
    @Override
    public PageBeanVo findRecommendUserByPage(Long toUserId, Integer pageNum, Integer pageSize) {
        Integer index = (pageNum - 1) * pageSize;
        Query query = new Query(

                Criteria.where("toUserId").is(toUserId)
        ).with(Sort.by(Sort.Order.desc("score"))).skip(index + 1).limit(pageSize);

        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);

        return new PageBeanVo(pageNum, pageSize, 0L, recommendUsers);
    }

    // 查询用户封面信息
    @Override
    public RecommendUser personalInfo(Long jiarenId, Long toUserId) {
        // 1.构建条件
        Query query = new Query(
                Criteria.where("userId").is(jiarenId)
                        .and("toUserId").is(toUserId)
        );
        // 2 .查询并返回缘分值
        return mongoTemplate.findOne(query, RecommendUser.class);

    }
    // 是否喜欢//todo 是否喜欢
    @Override
    public Boolean alreadyLove(Long likeUserId, Long userId) {

        Query query = new Query(
                Criteria.where("likeUserId").is(likeUserId).and("userId").is(userId)
        );
        UserLike userLike = mongoTemplate.findOne(query, UserLike.class);

        return userLike!=null;
    }
}
